/*
 * @Author: “ygnnli” “1585923072@qq.com”
 * @Date: 2024-08-03 17:28:20
 * @LastEditors: “ygnnli” “1585923072@qq.com”
 * @LastEditTime: 2024-08-03 19:52:50
 * @FilePath: /110/46/udp_echo_server/InetAddr.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <iostream>

// 网络四件套
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>

// 获取客户端的IP地址和端口号
class InetAddr
{

private:
    void GetAddress(std::string *ip, uint16_t *port)
    {
        *ip = inet_ntoa(_addr.sin_addr);
        *port = ntohs(_addr.sin_port);
    }

public:
    InetAddr(const struct sockaddr_in &addr) : _addr(addr)
    {
        GetAddress(&_ip, &_port);
    }
    std::string IP() { return _ip; }
    uint16_t Port() { return _port; }

    ~InetAddr() {}

private:
    struct sockaddr_in _addr; // 客户端地址
    std::string _ip;
    uint16_t _port;
};